import request from 'utils/request';

export interface PendingDriverQuery {
  page?: number;
  page_size?: number;
  search?: string;
  status?: 1 | 2 | 3; // 1-待审核，2-已驳回，3-已通过
  start_date?: string; // YYYY-MM-DD
  end_date?: string;   // YYYY-MM-DD
  sort_by?: string;
  sort_order?: 'asc' | 'desc';
}

export interface PendingDriverItem {
  id: string;
  driver_name: string;
  driver_phone: string;
  id_card_number: string;
  id_card_front_img: string;
  id_card_back_img: string;
  driver_license_number: string;
  driver_license_front_img: string;
  driver_license_back_img: string;
  driving_license_front_img: string;
  driving_license_back_img: string;
  driving_license_car_type: string;
  driving_license_car_length: number;
  driving_license_car_width: number;
  driving_license_car_height: number;
  car_images: string;
  car_plate: string;
  driving_license_max_load: number;
  driving_license_max_volume: number;
  driving_license_max_height: number;
  driving_license_vehicle_axles: string;
  driving_license_forbidden_area: string;
  driving_license_loading_method: string;
  driving_license_vehicle_requirements: string;
  driving_license_register_date: string;
  check_status: string; // "1" | "2" | "3"
  reject_reason: string | null;
  created_at: string;
  updated_at: string;
}

export interface PendingDriverResponse {
  code: 'OK' | number;
  msg?: string;
  data: {
    items: PendingDriverItem[];
    total: number;
    page: number;
    page_size: number;
    total_pages: number;
  };
}

export const getPendingDrivers = async (params: PendingDriverQuery) => {
  const res = await request.get<PendingDriverResponse>('/api/v1/drivers/pending', { params });
  return res;
};

// 已通过司机列表（与待审核返回结构一致）
export interface ApprovedDriverQuery {
  page?: number;
  page_size?: number;
  search?: string;
  sort_by?: string;
  sort_order?: 'asc' | 'desc';
}

export interface ApprovedDriverResponse {
  code: 'OK' | number;
  msg?: string;
  data: {
    items: PendingDriverItem[];
    total: number;
    page: number;
    page_size: number;
    total_pages: number;
  };
}

export const getApprovedDrivers = async (params: ApprovedDriverQuery) => {
  const res = await request.get<ApprovedDriverResponse>('/api/v1/drivers/approved', { params });
  return res;
};

export interface ReviewDriverBody {
  action: 'approve' | 'reject';
  reject_reason?: string;
}

export interface ReviewDriverResponse {
  code: 'OK' | number;
  msg?: string;
  data: {
    driver_id: string;
    action: 'approve' | 'reject';
    check_status: '1' | '2' | '3';
    reject_reason: string | null;
    reviewed_at: string;
  };
}

export const reviewDriver = async (driverId: string, body: ReviewDriverBody) => {
  const res = await request.post<ReviewDriverResponse>(`/api/v1/drivers/${driverId}/review`, body);
  return res;
};

export interface DriverDetailResponse {
  code: 'OK' | number;
  msg?: string;
  data: PendingDriverItem; // 与列表项字段基本一致
}

export const getDriverDetail = async (driverId: string) => {
  const res = await request.get<DriverDetailResponse>(`/api/v1/drivers/${driverId}`);
  return res;
};

export interface CreateDriverBody {
  driver_name: string;
  driver_phone: string;
  id_card_number: string;
  id_card_front_img?: string;
  id_card_back_img?: string;
  driver_license_number: string;
  driver_license_front_img?: string;
  driver_license_back_img?: string;
  driving_license_front_img?: string;
  driving_license_back_img?: string;
  driving_license_car_type?: string;
  driving_license_car_length?: number;
  driving_license_car_width?: number;
  driving_license_car_height?: number;
  driving_license_max_load?: number;
  driving_license_max_volume?: number;
  driving_license_max_height?: number;
  driving_license_vehicle_axles?: string;
  driving_license_forbidden_area?: string;
  driving_license_loading_method?: string;
  driving_license_vehicle_requirements?: string;
  car_images?: string;
  car_plate?: string;
  driving_license_register_date?: string; // YYYY-MM-DD
  check_status: '1' | '2' | '3';
}

export interface CreateDriverResponse {
  code: 'OK' | number;
  msg?: string;
  data: {
    id: string;
    driver_name: string;
    driver_phone: string;
    check_status: '1' | '2' | '3';
    created_at: string;
    updated_at: string;
  };
}

export const createDriver = async (body: CreateDriverBody) => {
  const res = await request.post<CreateDriverResponse>('/api/v1/drivers', body);
  return res;
};

// 更新已通过审核的司机信息
export interface UpdateDriverBody {
  // 司机基本信息
  driver_name?: string;
  driver_phone?: string;
  // 车辆基本信息
  car_plate?: string;
  driving_license_car_type?: string;
  // 车辆尺寸信息
  driving_license_car_length?: number;
  driving_license_car_width?: number;
  driving_license_car_height?: number;
  // 载重信息
  driving_license_max_load?: number;
  driving_license_max_volume?: number;
  driving_license_max_height?: number;
  // 车辆配置信息
  driving_license_vehicle_axles?: string;
  driving_license_forbidden_area?: string;
  driving_license_loading_method?: string;
  driving_license_vehicle_requirements?: string;
}

export interface UpdateDriverResponse {
  code: 'OK' | number;
  msg?: string;
  data: {
    id: string;
    driver_name: string;
    driver_phone: string;
    car_plate: string;
    driving_license_car_type: string;
    updated_at: string;
  };
}

export const updateDriver = async (driverId: string, body: UpdateDriverBody) => {
  const res = await request.put<UpdateDriverResponse>(`/api/v1/drivers/${driverId}`, body);
  return res;
};

// 删除司机
export interface DeleteDriverResponse {
  code: 'OK' | number;
  msg?: string;
  data: null;
}

export const deleteDriver = async (driverId: string) => {
  const res = await request.delete<DeleteDriverResponse>(`/api/v1/drivers/${driverId}`);
  return res;
};
